package cn.cvs.servlet.sysUser;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import cn.cvs.util.SpringUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "CheckOldPwdServlet", urlPatterns = "/sys/user/checkOldPwd")
public class CheckOldPwdServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        SysUserService sysUserService= (SysUserService) SpringUtil.getBean ( "sysUserService" );
        PrintWriter writer = response.getWriter ( );
        Map map=new HashMap<> (  );
        if (request.getParameter ( "oldPassword" )!=null||request.getParameter ( "oldPassword" )!="") {
            if (request.getSession ().getAttribute ( "userSession" )!=null){
                SysUser userSession = (SysUser) request.getSession ( ).getAttribute ( "userSession" );
                Integer id = userSession.getId ( );
                String oldPassword = request.getParameter ( "oldPassword" );
                if (sysUserService.getUserByIdAndPwd ( id,oldPassword )!=null){
                    map.put ( "result","true" );
                }else {
                    map.put ( "result","false" );
                }
            }else {
                map.put ( "result","sessionerror" );
            }
        }else {
            map.put ( "result","error" );
        }
        writer.print ( JSON.toJSONString(map) );
        writer.close ();
    }
}
